<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    {include file="../tpl/header.html" /}
</head>
<body>
{include file="../tpl/menu.html" /}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5" action="{:url('admin/admin/user')}">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="name" value="{$name}" placeholder="用户名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="phone" value="{$phone}" placeholder="手机号" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select name="role_id">
                                <option value="0">角色</option>
                                {volist name="role_list" id="vo"}
                                <option value="{$key}" {if condition="$key eq $role_id"}selected="selected"{/if}>{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select name="status">
                                <option value="0">状态</option>
                                <option value="1" {if condition="1 eq $status"}selected="selected"{/if}>启用</option>
                                <option value="2" {if condition="2 eq $status"}selected="selected"{/if}>禁用</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button title="搜索" class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button title="导出" class="layui-btn" onclick="export_excel();"><i class="icon iconfont"></i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    {if condition="check_auth('admin/admin/useradd')"}
                    <button class="layui-btn" onclick="xadmin.open('添加','{:url('admin/admin/userAdd')}')"><i class="layui-icon"></i>添加</button>
                    {/if}
                    {if condition="check_auth('admin/admin/userEditOp')"}
                    <button class="layui-btn" onclick="edit_all()"><i class="layui-icon"></i>批量编辑</button>
                    {/if}
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>
                                <input type="checkbox" lay-filter="check_all" name="" lay-skin="primary" />
                            </th>
                            <th>UID</th>
                            <th>登录名</th>
                            <th>用户名</th>
                            <th>手机号</th>
                            <th>角色</th>
                            <th>邮箱</th>
                            <th>错误次数</th>
                            <th>添加时间</th>
                            <th>单点登录</th>
                            <th>当前状态</th>
                            <th>操作</th></tr>
                        </thead>
                        <tbody>
                        {volist name="list" id="vo"}
                        <tr>
                            <td>
                                <input class="check_all" type="checkbox" name="id" value="{$vo['uid']}" lay-skin="primary">
                            </td>
                            <td>{$vo['uid']}</td>
                            <td>{$vo['username']}</td>
                            <td>{$vo['name']}</td>
                            <td>{$vo['phone']}</td>
                            <td>
                            {if condition="empty($role_list[$vo['role_id']])"}
                                无角色
                            {else/}
                                {$role_list[$vo['role_id']]['name']}
                            {/if}
                            </td>
                            <td>{$vo['email']}</td>
                            <td>{$vo['error_num']}</td>
                            <td>{$vo['add_time']}</td>
                            <td>
                                <input data-uid="{$vo['uid']}" value="{$vo.sso_status}" {if condition="$vo['sso_status'] eq 1"}checked="checked"{/if} lay-filter="switch_change_status" type="checkbox" name="switch" lay-text="开启|关闭" lay-skin="switch" />
                            </td>
                            <td class="td-status">
                                {if condition="$vo['status'] eq 1"}
                                <span data-field="uid" data-status="{$vo['status']}" onclick="set_status(this,{$vo['uid']},'{:url('admin/admin/setUserStatus')}');" title="点击更改为禁用" class="layui-btn layui-btn-normal layui-btn-mini">启用</span>
                                {else/}
                                <span data-field="uid" data-status="{$vo['status']}" onclick="set_status(this,{$vo['uid']},'{:url('admin/admin/setUserStatus')}');" title="点击更改为启用" class="layui-btn layui-btn-danger layui-btn-mini">禁用</span>
                                {/if}
                            </td>
                            <td class="td-manage">
                                {if condition="check_auth('admin/admin/userEdit')"}
                                <button title="编辑" class="layui-btn layui-bg-blue layui-btn-xs"  onclick="xadmin.open('编辑','{:url('admin/admin/userEdit',['uid' => $vo['uid']])}')" >
                                    <i class="layui-icon">&#xe642;</i>编辑
                                </button>
                                {/if}
                                {if condition="check_auth('admin/admin/userClearError')"}
                                <button title="清除错误次数" class="layui-btn layui-btn-warm layui-btn-xs"  onclick="request_result('{:url('admin/admin/userClearError',['uid' => $vo['uid']])}','确定要清除用户错误次数吗?')" >
                                    清除错误次数
                                </button>
                                {/if}
                                {if condition="check_auth('admin/admin/userDel')"}
                                <button title="删除" class="layui-btn-danger layui-btn layui-btn-xs" onclick="del(this,'{$vo.uid}','{:url('admin/admin/userDel',['uid' => $vo['uid']])}')" href="javascript:;" >
                                    <i class="layui-icon">&#xe640;</i>删除
                                </button>
                                {/if}
                            </td>
                        </tr>
                        {/volist}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        {$page_info|raw}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function(){
        var form = layui.form;
        form.on('switch(switch_change_status)', function(data){
            var obj = this;
            var value = $(obj).val();
            var uid = $(obj).data('uid');
            var s;
            if(value == 1){
                s = 2;
            }else{
                s = 1;
            }
            $.ajax({
                type:'post',
                data:{'uid':uid,'v':s},
                url:"{:url('admin/admin/setUserSso')}",
                dataType:'json',
                async:false,
                success:function(e){
                    if(e.code === 1){
                        $(obj).val(s);
                    }else{
                        layer.msg(e.msg,{icon:2,time: layer_msg_error_time});
                    }
                }
            });
        });

        // 监听全选
        form.on('checkbox(check_all)', function(data){
            if(data.elem.checked){
                //$('tbody input').prop('checked',true);
                $('.check_all').prop('checked',true);
            }else{
                //$('tbody input').prop('checked',false);
                $('.check_all').prop('checked',false);
            }
            form.render('checkbox');
        });
    });
    //批量修改,主要是传递id过去
    function edit_all() {
        var ids = [];

        // 获取选中的id
        //$('tbody input').each(function(index, el) {
        $('.check_all').each(function(index, el) {
            if($(this).prop('checked')){
                ids.push($(this).val())
            }
        });

        if(ids.toString() === ''){
            layer.msg('请选择需要编辑的UID');
            return;
        }

        xadmin.open('添加','{:url('admin/admin/userEditOp')}'+'?uid='+ids.toString());
    }
</script>
</body>
</html>